﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class AddColorProduct : System.Web.UI.UserControl
    {
        public int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                id = Convert.ToInt32(Request.QueryString["idproduct"]);
                DataTable dt = DAL.DCollector.DBCollector.Instance.SelectSQL(" Select Color_ID,Color_Name from tbl_Color Where Color_ID NOT IN(select Color_ID from tbl_ProductColor Where tbl_ProductColor.Product_ID = " + id + ")");
                rpt_Color.DataSource = dt;
                rpt_Color.DataBind();
            }
        }

        protected void btnAddColor_Click(object sender, EventArgs e)
        {
            id = Convert.ToInt32(Request.QueryString["idproduct"]);
            DataTable dt = DAL.DCollector.DBCollector.Instance.SelectSQL(" Select Color_ID,Color_Name from tbl_Color Where Color_ID NOT IN(select Color_ID from tbl_ProductColor Where tbl_ProductColor.Product_ID = " + id + ")");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_ProductColor", "ProductColor_ID"));
                CheckBox checkboxcolor = (CheckBox)rpt_Color.Items[i].FindControl("chkidColor");
                if (checkboxcolor.Checked == true)
                {
                    int idnextproductcolor = nextid + 1;
                    string filePathBigImage = "/Images/Image-Products/" + id + "" + Convert.ToInt32(dt.Rows[i]["Color_ID"].ToString()) + "a";
                    string filePathSmallIamge = "/Images/Image-Products/" + id + "" + Convert.ToInt32(dt.Rows[i]["Color_ID"].ToString()) + "b,/Images/Image-Products/" + id + "" + Convert.ToInt32(dt.Rows[i]["Color_ID"].ToString()) + "c,/Images/Image-Products/" + id + "" + Convert.ToInt32(dt.Rows[i]["Color_ID"].ToString()) + "d";
                    string value1 = "" + idnextproductcolor + "," + id + "," + Convert.ToInt32(dt.Rows[i]["Color_ID"].ToString()) + ",'" + filePathBigImage + "','" + filePathSmallIamge + "'";
                    List<string> values1 = new List<string>();
                    values1.Add(value1);
                    DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_ProductColor", "insertbyrows", "*", values1, false);
                    values1.Clear();
                }
            }
            Response.Redirect("/Admin/Index.aspx?panel=1&sub=1");
        }
    }
}